/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GestaoCadTurma.java
 *
 * Created on 22/08/2009, 08:43:43
 */

package siga.visao_academico;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.visao_busca.buscaCurso;
import siga.visao.index;
import siga.visao.verErros;

/**
 *
 * @author rafaelfrasson
 */
public class GestaoCadTurma extends javax.swing.JInternalFrame {
    Object[] JOopcoes = {"Sim","Não"};
    /** Creates new form GestaoCadTurma */
    public GestaoCadTurma() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpPTurma = new javax.swing.JPanel();
        barraFerramentaFonecedor = new javax.swing.JToolBar();
        btNovo = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btRemover = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        jpTabela = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtTurma = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jtfCodTurma = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfAno = new javax.swing.JTextField();
        jtfSemestre = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jtfTipoEnlace = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtfCodCurso = new javax.swing.JTextField();
        jtfDesCurso = new javax.swing.JTextField();
        jbBusca = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jtfBusca = new javax.swing.JTextField();
        jbBusca1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jtfTurno = new javax.swing.JComboBox();
        jtfFase = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();

        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Gerando Turmas");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/banco_16x16.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jpPTurma.setName("jpPTurma"); // NOI18N

        barraFerramentaFonecedor.setFloatable(false);
        barraFerramentaFonecedor.setRollover(true);
        barraFerramentaFonecedor.setName("barraFerramentaFonecedor"); // NOI18N

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/novo_32x32.png"))); // NOI18N
        btNovo.setMnemonic('N');
        btNovo.setText("Novo");
        btNovo.setToolTipText("Inseri um Novo Cadastro");
        btNovo.setFocusable(false);
        btNovo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btNovo.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btNovo.setName("btNovo"); // NOI18N
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btNovo);

        btAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/editar_32x32.png"))); // NOI18N
        btAlterar.setMnemonic('A');
        btAlterar.setText("Alterar");
        btAlterar.setToolTipText("Alterar um  Cadastro");
        btAlterar.setEnabled(false);
        btAlterar.setFocusable(false);
        btAlterar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btAlterar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btAlterar.setName("btAlterar"); // NOI18N
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btAlterar);

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/cancelar_32x32.png"))); // NOI18N
        btCancelar.setMnemonic('C');
        btCancelar.setText("Cancelar");
        btCancelar.setToolTipText("Cancelar um Cadastro");
        btCancelar.setEnabled(false);
        btCancelar.setFocusable(false);
        btCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btCancelar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btCancelar.setName("btCancelar"); // NOI18N
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btCancelar);

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/salvar_32x32.png"))); // NOI18N
        btSalvar.setMnemonic('S');
        btSalvar.setText("Salvar");
        btSalvar.setToolTipText("Salvar um Cadastro");
        btSalvar.setEnabled(false);
        btSalvar.setFocusable(false);
        btSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btSalvar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btSalvar.setName("btSalvar"); // NOI18N
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btSalvar);

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/remover_32x32.png"))); // NOI18N
        btRemover.setMnemonic('R');
        btRemover.setText("Remover");
        btRemover.setToolTipText("Remove um Cadastro");
        btRemover.setEnabled(false);
        btRemover.setFocusable(false);
        btRemover.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btRemover.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btRemover.setName("btRemover"); // NOI18N
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btRemover);

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/sair_32x32.png"))); // NOI18N
        btSair.setMnemonic('S');
        btSair.setText("Sair");
        btSair.setToolTipText("Sair da Janela");
        btSair.setFocusable(false);
        btSair.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btSair.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btSair.setName("btSair"); // NOI18N
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btSair);

        jpTabela.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados das Turmas"));
        jpTabela.setName("jpTabela"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtTurma.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Turma", "Ano", "S", "Enlace"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtTurma.setName("jtTurma"); // NOI18N
        jtTurma.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtTurmaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtTurma);
        jtTurma.getColumnModel().getColumn(1).setPreferredWidth(50);
        jtTurma.getColumnModel().getColumn(1).setMaxWidth(50);
        jtTurma.getColumnModel().getColumn(2).setPreferredWidth(40);
        jtTurma.getColumnModel().getColumn(2).setMaxWidth(40);
        jtTurma.getColumnModel().getColumn(3).setPreferredWidth(70);
        jtTurma.getColumnModel().getColumn(3).setMaxWidth(70);

        javax.swing.GroupLayout jpTabelaLayout = new javax.swing.GroupLayout(jpTabela);
        jpTabela.setLayout(jpTabelaLayout);
        jpTabelaLayout.setHorizontalGroup(
            jpTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
        );
        jpTabelaLayout.setVerticalGroup(
            jpTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
        );

        jLabel1.setText("Turma:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfCodTurma.setEditable(false);
        jtfCodTurma.setName("jtfCodTurma"); // NOI18N

        jLabel2.setText("Ano:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfAno.setEditable(false);
        jtfAno.setName("jtfAno"); // NOI18N

        jtfSemestre.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---", "A", "B" }));
        jtfSemestre.setEnabled(false);
        jtfSemestre.setName("jtfSemestre"); // NOI18N

        jLabel3.setText("Semestre:");
        jLabel3.setName("jLabel3"); // NOI18N

        jtfTipoEnlace.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---", "Turma", "Módulo", "Bimestre", "Ano", "Disciplina" }));
        jtfTipoEnlace.setEnabled(false);
        jtfTipoEnlace.setName("jtfTipoEnlace"); // NOI18N

        jLabel4.setText("Ensalamento:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel6.setText("Curso:");
        jLabel6.setName("jLabel6"); // NOI18N

        jtfCodCurso.setEditable(false);
        jtfCodCurso.setName("jtfCodCurso"); // NOI18N

        jtfDesCurso.setEditable(false);
        jtfDesCurso.setName("jtfDesCurso"); // NOI18N

        jbBusca.setFont(new java.awt.Font("Dialog", 0, 10));
        jbBusca.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBusca.setText("Buscar");
        jbBusca.setEnabled(false);
        jbBusca.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbBusca.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jbBusca.setName("jbBusca"); // NOI18N
        jbBusca.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbBusca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscaActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Localizar"));
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel9.setText("Turma:");
        jLabel9.setName("jLabel9"); // NOI18N

        jtfBusca.setName("jtfBusca"); // NOI18N

        jbBusca1.setFont(new java.awt.Font("Dialog", 0, 10));
        jbBusca1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBusca1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbBusca1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jbBusca1.setName("jbBusca1"); // NOI18N
        jbBusca1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbBusca1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBusca1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jtfBusca, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbBusca1)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jbBusca1)
                    .addComponent(jtfBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jLabel5.setText("Turno:");
        jLabel5.setName("jLabel5"); // NOI18N

        jtfTurno.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Noturno" }));
        jtfTurno.setEnabled(false);
        jtfTurno.setName("jtfTurno"); // NOI18N

        jtfFase.setEditable(false);
        jtfFase.setName("jtfFase"); // NOI18N

        jLabel7.setText("Fase:");
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout jpPTurmaLayout = new javax.swing.GroupLayout(jpPTurma);
        jpPTurma.setLayout(jpPTurmaLayout);
        jpPTurmaLayout.setHorizontalGroup(
            jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(barraFerramentaFonecedor, javax.swing.GroupLayout.DEFAULT_SIZE, 968, Short.MAX_VALUE)
            .addGroup(jpPTurmaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jpTabela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jtfTurno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpPTurmaLayout.createSequentialGroup()
                        .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpPTurmaLayout.createSequentialGroup()
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtfCodTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jtfAno, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel7)
                                    .addComponent(jtfFase, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jtfSemestre, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jtfTipoEnlace, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel4)))
                            .addGroup(jpPTurmaLayout.createSequentialGroup()
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jtfCodCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfDesCurso, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbBusca)))
                .addContainerGap())
        );
        jpPTurmaLayout.setVerticalGroup(
            jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpPTurmaLayout.createSequentialGroup()
                .addComponent(barraFerramentaFonecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpPTurmaLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jpTabela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jpPTurmaLayout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jpPTurmaLayout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jtfCodCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jtfDesCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jbBusca))
                        .addGap(18, 18, 18)
                        .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jpPTurmaLayout.createSequentialGroup()
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jtfSemestre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jtfTipoEnlace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jpPTurmaLayout.createSequentialGroup()
                                .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jpPTurmaLayout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jtfFase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jpPTurmaLayout.createSequentialGroup()
                                        .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jpPTurmaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jtfCodTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jtfAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfTurno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpPTurma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpPTurma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        btSalvar.setEnabled(!btSalvar.isEnabled());
        btCancelar.setEnabled(!btCancelar.isEnabled());
        btNovo.setEnabled(!btNovo.isEnabled());
        btAlterar.setEnabled(false);
        btRemover.setEnabled(false);
        gerenciaCampos();
        LimpaCampos();
}//GEN-LAST:event_btNovoActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        gerenciaBotao();
        gerenciaCampos();
}//GEN-LAST:event_btAlterarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        if(btCancelar.isEnabled()){
            if(!btNovo.isEnabled()){
                btSalvar.setEnabled(!btSalvar.isEnabled());
                btCancelar.setEnabled(!btCancelar.isEnabled());
                btNovo.setEnabled(!btNovo.isEnabled());
            }else{
                gerenciaBotao();
            }
            gerenciaCampos();
            LimpaCampos();
        }
}//GEN-LAST:event_btCancelarActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if(!jtfCodTurma.getText().equals("")){
            if(!jtfAno.getText().equals("")){
                if(jtfSemestre.getSelectedIndex()!=0){
                    if(jtfTipoEnlace.getSelectedIndex()!=0){
                        if(!jtfDesCurso.getText().equals("")){
                            conexao.Inserir("replace into grade_turma (codigo,cod_emp,cod_fase,ano,semestre,enlace,cod_curso,turno) " +
                                    " values(\""+jtfCodTurma.getText()+"\","+index.cod_emp+","+jtfFase.getText()+","+jtfAno.getText()+",\""+jtfSemestre.getSelectedItem()+"\",\""+jtfTipoEnlace.getSelectedItem()+"\","+jtfCodCurso.getText()+",\""+jtfTurno.getSelectedItem()+"\")");
                            gerenciaBotao();
                            gerenciaCampos();
                            LimpaCampos();
                            CarregaTabela();
                        }else{
                            JOptionPane.showMessageDialog(null, "Campo Curso é obrigatório.","Alerta do Sistema.",JOptionPane.INFORMATION_MESSAGE);
                            jtfCodCurso.requestFocus();
                        }
                    }else{
                        JOptionPane.showMessageDialog(null, "Campo Ensalamento é obrigatório.","Alerta do Sistema.",JOptionPane.INFORMATION_MESSAGE);
                        jtfTipoEnlace.requestFocus();
                    }
                }else{
                    JOptionPane.showMessageDialog(null, "Campo Semestre é obrigatório.","Alerta do Sistema.",JOptionPane.INFORMATION_MESSAGE);
                    jtfSemestre.requestFocus();
                }
            }else{
                JOptionPane.showMessageDialog(null, "Campo Ano é obrigatório.","Alerta do Sistema.",JOptionPane.INFORMATION_MESSAGE);
                jtfAno.requestFocus();
            }
        }else{
            JOptionPane.showMessageDialog(null, "Campo Turma é obrigatório.","Alerta do Sistema.",JOptionPane.INFORMATION_MESSAGE);
            jtfCodTurma.requestFocus();
        }
}//GEN-LAST:event_btSalvarActionPerformed

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
       if (JOptionPane.showOptionDialog(null, "Deseja remover essa Turma?\nEsta ação implica em remover todos os registros de Turmas, Diários.", "Removendo Curso", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, JOopcoes, JOopcoes[0]) == JOptionPane.YES_OPTION) {
            try {
                conexao.stm.executeUpdate("delete from grade_turma where codigo=\""+jtfCodTurma.getText().substring(0,(jtfCodTurma.getText().length()-2))+"\" and cod_emp="+index.cod_emp+"");
                LimpaCampos();
                CarregaTabela();
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }
        }
}//GEN-LAST:event_btRemoverActionPerformed

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        btCancelarActionPerformed(evt);
        setVisible(false);
        dispose();
}//GEN-LAST:event_btSairActionPerformed

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed

    }//GEN-LAST:event_formInternalFrameClosed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        CarregaTabela();
        jtfAno.setText(new SimpleDateFormat("yyyy").format(new Date()));
    }//GEN-LAST:event_formInternalFrameOpened

    private void jtTurmaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtTurmaMouseClicked
        try {
            if (btNovo.isEnabled()) {
                btAlterar.setEnabled(true);
                btRemover.setEnabled(true);
            }
            javax.swing.table.DefaultTableModel jtt = (javax.swing.table.DefaultTableModel) jtTurma.getModel();
            jtfCodTurma.setText("" + jtt.getValueAt(jtTurma.getSelectedRow(), 0).toString().substring(0, jtt.getValueAt(jtTurma.getSelectedRow(), 0).toString().length()-2));
            jtfFase.setText("" + jtt.getValueAt(jtTurma.getSelectedRow(), 0).toString().substring(jtt.getValueAt(jtTurma.getSelectedRow(), 0).toString().length()-1, jtt.getValueAt(jtTurma.getSelectedRow(), 0).toString().length()));
            jtfAno.setText(jtt.getValueAt(jtTurma.getSelectedRow(), 1).toString());
            jtfSemestre.setSelectedItem(jtt.getValueAt(jtTurma.getSelectedRow(), 2).toString());
            jtfTipoEnlace.setSelectedItem(jtt.getValueAt(jtTurma.getSelectedRow(), 3).toString());
            conexao.Query("select codigo,descricao from grade_curso where cod_emp=" + index.cod_emp + " and codigo=" + jtfCodTurma.getText().substring(0, 3));
            conexao.rs.next();
            jtfCodCurso.setText(conexao.rs.getString("codigo"));
            jtfDesCurso.setText(conexao.rs.getString("descricao"));
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }

    }//GEN-LAST:event_jtTurmaMouseClicked

    private void jbBuscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscaActionPerformed
        buscaCurso bc = new buscaCurso(null,true);
        bc.setLocation((index.tela.width-bc.getWidth())/2, (index.tela.height-bc.getHeight())/2);
        bc.setVisible(true);
        if(buscaCurso.isConfirmaCurso()){
            try {
                jtfCodCurso.setText(index.LRcurso.get(0).toString());
                jtfDesCurso.setText(index.LRcurso.get(1).toString());
                conexao.Query("select substring(max(codigo),length(max(codigo)),length(max(codigo))) as turma from grade_turma where cod_emp=" + index.cod_emp + " and codigo like \""+index.LRcurso.get(0)+"."+jtfAno.getText()+".%"+"\"");
                conexao.rs.next();
                jtfCodTurma.setText((conexao.rs.getString("turma")==null)?index.LRcurso.get(0)+"."+jtfAno.getText()+".1":index.LRcurso.get(0)+"."+jtfAno.getText()+"."+(conexao.rs.getInt("turma")+1));
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }
        }
        index.LRcurso.add(0,"");
        index.LRcurso.add(1,"");
    }//GEN-LAST:event_jbBuscaActionPerformed

    private void jbBusca1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBusca1ActionPerformed
        CarregaTabela();
    }//GEN-LAST:event_jbBusca1ActionPerformed

    private void gerenciaCampos(){
        jtfCodCurso.setEditable(!jtfAno.isEditable());
        jtfFase.setEditable(!jtfAno.isEditable());
        jtfAno.setEditable(!jtfAno.isEditable());
        jtfTipoEnlace.setEnabled(!jtfSemestre.isEnabled());
        jbBusca.setEnabled(!jtfSemestre.isEnabled());
        jtfSemestre.setEnabled(!jtfSemestre.isEnabled());
    }

    private void gerenciaBotao(){
        btNovo.setEnabled(!btNovo.isEnabled());
        btAlterar.setEnabled(!btAlterar.isEnabled());
        btCancelar.setEnabled(!btCancelar.isEnabled());
        btRemover.setEnabled(!btRemover.isEnabled());
        btSalvar.setEnabled(!btSalvar.isEnabled());
    }

    private void LimpaCampos(){
        jtfCodTurma.setText("");
        jtfFase.setText("");
        jtfAno.setText(new SimpleDateFormat("yyyy").format(new Date()));
        jtfCodCurso.setText("");
        jtfDesCurso.setText("");
        jtfSemestre.setSelectedIndex((Integer.parseInt(new SimpleDateFormat("MM").format(new Date()))<=6)?1:2);
        jtfTipoEnlace.setSelectedIndex(0);
    }

    private void CarregaTabela(){
        try {
            javax.swing.table.DefaultTableModel jtt = (javax.swing.table.DefaultTableModel) jtTurma.getModel();
            for (int x = jtTurma.getRowCount() - 1; x >= 0; --x) {
                jtt.removeRow(x);
            }
            String busca=(jtfBusca.getText().equals(""))?"":" and codigo like \""+jtfBusca.getText()+"%\"";
            conexao.Query("select * from grade_turma where cod_emp="+index.cod_emp+busca+" and ano="+index.periodo.get(0) +" and semestre=\""+index.periodo.get(1)+"\" order by ano DESC");
            while(conexao.rs.next()){
                jtt.addRow(new Object[]{conexao.rs.getString("codigo")+"/"+conexao.rs.getString("cod_fase"),conexao.rs.getString("ano"),conexao.rs.getString("semestre"),conexao.rs.getString("enlace")});
            }
        }catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar barraFerramentaFonecedor;
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton btSair;
    private javax.swing.JButton btSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbBusca;
    private javax.swing.JButton jbBusca1;
    private javax.swing.JPanel jpPTurma;
    private javax.swing.JPanel jpTabela;
    private javax.swing.JTable jtTurma;
    private javax.swing.JTextField jtfAno;
    private javax.swing.JTextField jtfBusca;
    private javax.swing.JTextField jtfCodCurso;
    private javax.swing.JTextField jtfCodTurma;
    private javax.swing.JTextField jtfDesCurso;
    private javax.swing.JTextField jtfFase;
    private javax.swing.JComboBox jtfSemestre;
    private javax.swing.JComboBox jtfTipoEnlace;
    private javax.swing.JComboBox jtfTurno;
    // End of variables declaration//GEN-END:variables

}
